package com.zzyl.nursing.task;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaweicloud.sdk.core.internal.model.Project;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.serve.domain.NursingElder;
import com.zzyl.serve.domain.NursingTask;
import com.zzyl.serve.mapper.NursingElderMapper;
import com.zzyl.serve.service.INursingProjectService;
import com.zzyl.serve.service.INursingTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@Slf4j
public class AlertTask2 {

    @Autowired
    private INursingTaskService nursingTaskService;
    @Autowired
    private INursingProjectService nursingProjectService;

    @Autowired
    private IElderService elderService;

    @Autowired
    private NursingElderMapper nursingElderMapper;


//    @Scheduled(cron = "0 * * * * ?")
    public void insertNursingTask() {


        boolean projectTask = getProjectTask();
        if (projectTask) {
            log.info("定时新增护理任务执行成功");
        } else {
            throw new BaseException("没有可执行项目");
        }

//            // 可以设置一些默认值或从其他服务获取数据
//            // 设置必需字段
//            nursingTask.setProjectId(1); // 根据实际项目设置
//            nursingTask.setCreateBy("1");
//
//
//            // 使用真实数据或预设数据
//            nursingTask.setProjectName("血压测量");
//            nursingTask.setElderId(1001L);
//            nursingTask.setElderName("李大爷");
//            nursingTask.setBedNumber("A101");
//            nursingTask.setEstimatedServerTime(LocalDateTime.now().plusHours(2));
//            nursingTaskService.insertNursingTask(nursingTask);
//            log.info("定时新增护理任务执行成功");
//        } catch (Exception e) {
//            log.error("定时新增护理任务执行失败", e);

    }

    private boolean getProjectTask() {
        //1. 找到所有已经入住的老人Set存储
        List<Elder> list = elderService.list(Wrappers.<Elder>lambdaQuery().eq(Elder::getStatus, 1));
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        Set<Elder> elderSet = new HashSet<>();

        for (Elder elder : list) {
            elderSet.add(elder);
        }
        //2. 根据老人id查询老人表，获取项目id和项目名称
        for (Elder elder : elderSet) {
            boolean bool = false;
            NursingTask nursingTask = new NursingTask();

            nursingTask.setStatus(1); // 设置初始状态为待执行
            //获取当前登录人
//            Long userId = getLoginUser().getUserId();
//            nursingTask.setCreateBy(String.valueOf(userId));
            nursingTask.setCreateBy("1");
            nursingTask.setEstimatedServerTime(LocalDateTime.now().plusHours(2));//设置预期执行时间

            nursingTask.setElderId(elder.getId());
            nursingTask.setElderName(elder.getName());
            nursingTask.setBedNumber(elder.getBedNumber());


            List<Project> projectList = elderService.selectElderProject(elder);
            if (CollectionUtils.isEmpty(projectList)) {
                bool = false;
                continue;

            } else {
                bool = true;
            }

            if (bool) {
                for (Project project : projectList) {
                    if (ObjectUtil.isEmpty(project)){
                        continue;
                    }
                    nursingTask.setProjectId(Integer.valueOf(project.getId()));
                    nursingTask.setProjectName(project.getName());

                    //找护理员
                    List<NursingElder> nursingElders = nursingElderMapper.selectList(Wrappers.<NursingElder>lambdaQuery()
                            .eq(NursingElder::getElderId, elder.getId()));
                    if (CollectionUtils.isEmpty(nursingElders)) {
                        continue;
                    }
                    for (NursingElder nursingElder : nursingElders) {
                        nursingTask.setNursingId(String.valueOf(nursingElder.getNursingId()));
                        return nursingTaskService.save(nursingTask);
                    }

                }
            }
        }
        return false;
    }
}